
/**
 * get
 * @param {string} url 
 * @param {object} params 
 * @param {function} callabck 
 * @param {string|undefined} type 
 * @param {object} headerFn 
 */

function get (url,params,callback,type,headers){
    // let headerFn = (xhr) =>{
    //     xhr.setRequestHeader('token','adf7cbdcdc62b07d94f86339e5687ca51')
    // }
    // let callback = res =>{       
    // }
    const xhr = new XMLHttpRequest
    xhr.onreadystatechange = () =>{
        if(xhr.readyState === 4){
            if(xhr.status === 200){
                let res = xhr.responseText
                if(type ==='json') res = JSON.parse(res)
                callback(res)
            }else{
                console.log('你有瑕疵：',xhr.status)
            }
        }
    }
    let temp =[]
    for(let key in params){
        temp.push(`${key}=${params[key]}`)
    }
    xhr.open('get',`${url}?${temp.join('&')}`)
    for (let key in Headers){
        xhr.setRequestHeader(key,headers[key])
    }
    xhr.send(null)
}
/**
 * post
 * @param {string} url 
 * @param {object} params 
 * @param {function} callback 
 * @param {string|undefined} type 
 * @param {object} headers 
 */
function post(url,params,callback,type,headers){
    const xhr = new XMLHttpRequest
    xhr.onreadystatechange = () =>{
        if(xhr.readyState === 4){
            if(xhr.status === 200){
                let res = xhr.responseText
                if(type ==='json') res = JSON.parse(res)
                callback(res)
            }else{
                console.log('你有瑕疵：',xhr.status)
            }
        }
    }
    let temp =[]
    for(let key in params){
        temp.push(`${key}=${params[key]}`)
    }
    xhr.open('get',`${url}`)
    xhr.setRequestHeader('content-type','application/x-www-form-encosed')
    for (let key in Headers){
        xhr.setRequestHeader(key,headers[key])
    }
    xhr.send(temp.join('&'))
}